IPHONE_IP=127.0.0.1

SDK=/usr/toolchain2/sys
CC=/usr/toolchain2/pre/bin/arm-apple-darwin9-g++
LD=$(CC)
LDFLAGS += -lobjc
LDFLAGS += -framework CoreFoundation
LDFLAGS += -framework Foundation
LDFLAGS += -framework UIKit
#LDFLAGS += -framework CoreGraphics
#LDFLAGS += -framework QuartzCore
#LDFLAGS += -framework GraphicsServices
#LDFLAGS += -framework CoreSurface
#LDFLAGS += -framework CoreAudio
#LDFLAGS += -framework Celestial
LDFLAGS += -framework AudioToolbox
#LDFLAGS += -framework WebCore
#LDFLAGS += -framework WebKit
#LDFLAGS += -framework SystemConfiguration
LDFLAGS += -isysroot $(SDK)
LDFLAGS += -F"$(SDK)/System/Library/Frameworks"
LDFLAGS += -F"$(SDK)/System/Library/PrivateFrameworks"
LDFLAGS += -lsqlite3.0
LDFLAGS += -bind_at_load
LDFLAGS += -multiply_defined suppress
LDFLAGS += -march=armv6
LDFLAGS += -mcpu=arm1176jzf-s

CFLAGS += -isysroot $(SDK)
CFLAGS += -F"$(SDK)/System/Library/Frameworks"
CFLAGS += -F"$(SDK)/System/Library/PrivateFrameworks"
CFLAGS += -Wall
CFLAGS += -x objective-c++
CFLAGS += -Diphoneos_version_min=2.0
CFLAGS += -DDEBUG=1

PROJECTNAME=MantuPlay
APPFOLDER=$(PROJECTNAME).app

BUILDDIR=./Build
EXCUTABLEDIR=$(BUILDDIR)/$(PROJECTNAME)
SRCDIR=./Classes
RESDIR=./Resources
OBJSDIR=./Objects

OBJS=$(notdir $(patsubst %.m,%.o,$(wildcard $(SRCDIR)/*.m)))
OBJS+=$(notdir $(patsubst %.c,%.o,$(wildcard $(SRCDIR)/*.c)))

RESOURCES=$(wildcard $(RESDIR)/*)
#RESOURCES+=$(wildcard ./*.png)

all: $(EXCUTABLEDIR)
	export CODESIGN_ALLOCATE=/usr/toolchain2/pre/bin/arm-apple-darwin9-codesign_allocate; ldid -S $(EXCUTABLEDIR)

$(EXCUTABLEDIR): $(addprefix $(OBJSDIR)/,$(OBJS))
	$(LD) $(LDFLAGS) -o $@ $^

$(OBJSDIR)/%.o: $(SRCDIR)/%.m
	$(CC) -c $(CFLAGS) $< -o $@
	
$(OBJSDIR)/%.o: $(SRCDIR)/%.c
	$(CC) -c $(CFLAGS) $< -o $@

dist:
	mkdir -p $(BUILDDIR)/$(APPFOLDER)
	cp Info.plist $(BUILDDIR)/$(APPFOLDER)/Info.plist
	@echo "APPL????" > $(BUILDDIR)/$(APPFOLDER)/PkgInfo
	cp $(RESOURCES) -t $(BUILDDIR)/$(APPFOLDER)/
	cp $(EXCUTABLEDIR) $(BUILDDIR)/$(APPFOLDER)

install: dist
	ssh root@$(IPHONE_IP) 'rm -fr /Applications/$(APPFOLDER)'
	scp -r $(BUILDDIR)/$(APPFOLDER) root@$(IPHONE_IP):/Applications/$(APPFOLDER)
	@echo "Application $(APPFOLDER) installed"
	
installbin:
	ssh root@$(IPHONE_IP) 'rm -fr /Applications/$(APPFOLDER)/$(PROJECTNAME)'
	scp -r $(EXCUTABLEDIR) root@$(IPHONE_IP):/Applications/$(APPFOLDER)/$(PROJECTNAME)

uninstall:
	ssh root@$(IPHONE_IP) 'rm -fr /Applications/$(APPFOLDER)'
	
uninstallbin:
	ssh root@$(IPHONE_IP) 'rm -fr /Applications/$(APPFOLDER)/$(PROJECTNAME)'

clean:
	@rm -f ./*.o
	@rm -f $(SRCDIR)/*.o
	@rm -f $(OBJSDIR)/*.o
	@rm -f $(BUILDDIR)/$(PROJECTNAME)